package com.imodia.checkoo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.imodia.checkoo.bean.CmsDepartmentBean;
import com.imodia.checkoo.bean.CmsUserBean;
import com.imodia.checkoo.service.impl.CmsDepartmentServiceImpl;
import com.imodia.checkoo.service.impl.CmsUserServiceImpl;


@Controller
@RequestMapping("/hfmanager/user/list.do")
public class CmsUserController extends BaseAdminController{

	private final String baseUrl = "user";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_selfredirect = "/hfmanager/" +baseUrl + "/list.do?action=editSelf";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";
	private final String url_list4acticle = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list4acticle";
	private final String url_list_select = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list_select";
	private final String url_self_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_self_edit";

	

	@Autowired
	private CmsUserServiceImpl cmsUserService;
	@Autowired
	private CmsDepartmentServiceImpl cmsDepartmentService;
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsUserService.delete(id);
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsUserBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsUserService.update(obj);
		}else{
			x = cmsUserService.insert(obj);
			obj.setId(x);
		}
		if(x>0)x=1;
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&x="+x+"&id="+obj.getId()), null);
	}
	
	@RequestMapping(params = "action=saveSelf")
	public ModelAndView saveSelf(@ModelAttribute("userMessage")CmsUserBean obj, HttpServletRequest request) {
		int x  = cmsUserService.update(obj);
		
		if(x>0)x=1;
		return new ModelAndView(new RedirectView(request.getContextPath()+url_selfredirect+"&x="+x+"&id="+obj.getId()), null);
	}
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request,CmsUserBean obj) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();

		m.put("info", cmsUserService.get(id));
		
		SqlParam sql = new SqlParam();
		if(super.getAdminId(request)!=1){
			sql.andNotEqualTo("id","1");
		}
		List<CmsDepartmentBean> department = cmsDepartmentService.list(sql);
		m.put("department", department);
		
	    return new ModelAndView(url_edit, m);
	}
	
	@RequestMapping(params = "action=editSelf")
	public ModelAndView editSelf(HttpServletRequest request,CmsUserBean obj) {
		int id=super.getAdminId(request);
		Map<String,Object> m = new HashMap<String,Object>();

		m.put("info", cmsUserService.get(id));
		
		List<CmsDepartmentBean> department = cmsDepartmentService.list(new SqlParam());
		m.put("department", department);
	    return new ModelAndView(url_self_edit, m);
	}
	
	
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsUserBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		
		SqlParam sql = new SqlParam();
		if(super.getAdminId(request)!=1){
			sql.andNotEqualTo("id","1");
		}
		
		PageResult<CmsUserBean> pageResult =cmsUserService.page(sql,page);
		model.put("page", pageResult);
		
		return new ModelAndView(url_list, model);
	}
	
	@RequestMapping(params = "action=list4acticle")
	public ModelAndView list4acticle(HttpServletRequest request, CmsUserBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
	
		PageResult<CmsUserBean> pageResult =cmsUserService.page(new SqlParam(),page);
		model.put("page", pageResult);
		
		return new ModelAndView(url_list4acticle, model);
	}
	
	@RequestMapping(params = "action=listSelect")
	public ModelAndView listSelect(HttpServletRequest request, CmsUserBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		SqlParam sql = new SqlParam();
		if(super.getAdminId(request)!=1){
			sql.andNotEqualTo("id","1");
		}
		
		PageResult<CmsUserBean> pageResult =cmsUserService.page(new SqlParam(),page);
		model.put("page", pageResult);
		return new ModelAndView(url_list_select, model);
	}
}
